package com.aisino.inveli.vinda.controller;

import com.aisino.inveli.vinda.entity.Customer;
import com.aisino.inveli.vinda.service.CustomerService;
import de.felixroske.jfxsupport.FXMLController;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import org.springframework.beans.factory.annotation.Autowired;

import javax.annotation.Resource;
import java.net.URL;
import java.util.List;
import java.util.ResourceBundle;

/**
 * @author leifeng
 * @Date 2017-11-21
 **/
@FXMLController
public class CustomerSelectController {

    @FXML
    private TableView<Customer> customerView;
    @FXML
    private TableColumn<Customer,Integer> id;
    @FXML
    private TableColumn<Customer,String> cusName;
    @FXML
    private TableColumn<Customer,String> regNumber;
    @FXML
    private TableColumn<Customer,String> bank;
    @FXML
    private TableColumn<Customer,String> addAndTel;
    @FXML
    private TableColumn<Customer,String> email;


    /**
     * 初始化客户列表
     * @param list
     */
    public void initData(List<Customer> list,CustomerService customerService) {
        id.setCellValueFactory(new PropertyValueFactory<>("id"));
        cusName.setCellValueFactory(new PropertyValueFactory<>("cusName"));
        regNumber.setCellValueFactory(new PropertyValueFactory<>("regNumber"));
        bank.setCellValueFactory(new PropertyValueFactory<>("bank"));
        addAndTel.setCellValueFactory(new PropertyValueFactory<>("addAndTel"));
        email.setCellValueFactory(new PropertyValueFactory<>("email"));
        customerView.setOnMouseClicked(event -> {
            if (event.getClickCount() == 2){
                Customer customer = customerView.getSelectionModel().getSelectedItem();
                System.out.println(1);
                //每次双击修改前把之前的所有客户selected属性改为false，然后修改当前选择客户selected
                customerService.updateCustomerSelect(customer);
                ((Node)event.getSource()).getScene().getWindow().hide();
            }
        });
        ObservableList<Customer> oList = FXCollections.observableArrayList();
        for (Customer cus: list) {
            oList.add(cus);
        }
        customerView.setItems(oList);
    }

}
